	<?php
	var_dump($_POST);
	//Api Youtube (Google code) http://code.google.com/apis/youtube/2.0/developers_guide_protocol.html
	if(isset($_POST['set_cat_search'])){
		$type_search =$_POST['set_cat_search'];
	}
	if(isset($_POST['set_txt_search'])){
		$type_search = 'search';
		$keyword = $_POST['set_txt_search'];
		//var_dump($keyword);
	}
	switch ($type_search){
		
		case 'search':
				$max_result = 24;
				
				$keyword = ereg_replace('[[:space:]]+', '/', trim($keyword));
				//$keyword = str_replace('[[:space:]]+', '/', trim($keyword));
				//Search with keyword;
				$feedURL = "http://gdata.youtube.com/feeds/api/videos/-/".$keyword."
				?orderby=viewCount&max-results=".$max_result;
				// read feed into SimpleXML object
				
				$sxml = simplexml_load_file($feedURL);
				//var_dump($sxml);
				// get summary counts from opensearch: namespace
				$counts = $sxml->children('http://a9.com/-/spec/opensearchrss/1.0/');
				$total = $counts->totalResults; 
				$startOffset = $counts->startIndex; 
				$endOffset = ($startOffset-1) + $counts->itemsPerPage; 
				break;
		case 'top-five':
				// set URL for XML feed containing category list
				$catURL = 'http://gdata.youtube.com/schemas/2007/categories.cat';

				// retrieve category list using atom: namespace
				// note: you can cache this list to improve performance, 
				// as it doesn't change very often!
				$cxml = simplexml_load_file($catURL);
				$cxml->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom');
				$categories = $cxml->xpath('//atom:category');
				
				break;
		case 'Travel':
				$feedURL = 'http://gdata.youtube.com/feeds/api/videos/-/Travel/';
    
				// read feed into SimpleXML object
				$sxml = simplexml_load_file($feedURL);
				
				// get summary counts from opensearch: namespace
				$counts = $sxml->children('http://a9.com/-/spec/opensearchrss/1.0/');
				$total = $counts->totalResults;  
				break;
		default:
			switch ($type_search){
				case 'most_viewed':
						$feedURL = 'http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed';//.$type_search;
						break;
				case 'top_rated':
						$feedURL = 'http://gdata.youtube.com/feeds/api/standardfeeds/top_rated';//.$type_search;
						break;
				case 'most_shared':
						$feedURL = 'http://gdata.youtube.com/feeds/api/standardfeeds/most_shared';//.$type_search;
						break;
				case 'most_shared':
						$feedURL = 'http://gdata.youtube.com/feeds/api/standardfeeds/most_shared';//.$type_search;
						break;	
				case 'most_recent':
						$feedURL = 'http://gdata.youtube.com/feeds/api/standardfeeds/most_recent';//.$type_search;
						break;
				case 'recently_featured':
						$feedURL = 'http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured';//.$type_search;
						break;
				case 'on_the_web':
						$feedURL = 'http://gdata.youtube.com/feeds/api/standardfeeds/on_the_web';//.$type_search;
						break;	
				default:
					$feedURL = 'http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed';//.$type_search;
					break;	
			}
			$sxml = simplexml_load_file($feedURL);
			break;
	}
	//Save result
	$results = array();
	//try{
	//	throw new Exception('Error connect api youtube.');	
	$i=0;
	if($type_search !='top-five'){
		foreach ($sxml->entry as $entry) {
		  // get nodes in media: namespace for media information
		  $media = $entry->children('http://search.yahoo.com/mrss/');
		  
		  // get video player URL
		  $attrs = $media->group->player->attributes();
		  $watch = $attrs['url']; 
		  //var_dump($watch);
		  
		  // get video thumbnail
		  $attrs = $media->group->thumbnail[0]->attributes();
		  $thumbnail = $attrs['url']; 
				
		  // get <yt:duration> node for video length
		  $yt = $media->children('http://gdata.youtube.com/schemas/2007');
		  $attrs = $yt->duration->attributes();
		  $length = $attrs['seconds']; 
		  
		  // get <yt:stats> node for viewer statistics
		  $yt = $entry->children('http://gdata.youtube.com/schemas/2007');
		  $attrs = $yt->statistics->attributes();
		  $viewCount = $attrs['viewCount']; 
		  
		  // get <gd:rating> node for video ratings
		  $gd = $entry->children('http://schemas.google.com/g/2005'); 
		  if ($gd->rating) {
			$attrs = $gd->rating->attributes();
			$rating = $attrs['average']; 
		  } else {
			$rating = 0; 
		  }

			$str = '&feature=youtube_gdata_player';
			$replate = '&amp;hl=en&amp;fs=1&amp;autoplay=1;loop =1;repeat=1';
			$watch_video = str_replace($str,$replate,$watch,$count);
			$str = 'http://www.youtube.com/watch?v=';
			$replate = 'http://www.youtube.com/v/';
			$watch_video = str_replace($str,$replate,$watch_video,$count);
			//
			$results[$i]['url']			= $watch_video;
			$results[$i]['title']			= $media->group->title;
			$results[$i]['author_name'] 	=$entry->author->name;
			$results[$i]['viewCount'] 	=$viewCount;
			$results[$i]['thumbnail']		= $thumbnail;
			$results[$i]['rating']  		= $rating;
			$results[$i]['description']	=  $media->group->description;
			$i++;
			}
		}
	;?>

<div  class="image_thumb">
		<?php if($type_search !='top-five'){ ?>
		<h2 class="result_search">Result search...</h2>
		<?php
			if($type_search == "Travel"){
				echo $total.' items found. Showing items '.$startOffset.' to '.$endOffset; }
			else if($type_search == "search"){
				echo $total.' items found.';
			}
			?>
		<ul>
		<?php
			if(isset($results)){
				
				foreach($results as $result){
			;?>
				<li>
					<a href="<?php echo $result['url'];?>"><img src="<?php echo $result['thumbnail'];?>" /></a>
					<div class="block">
						<h2><a href="<?php echo $result['url']; ?>"><?php echo substr($result['title'],0,50)."..."; ?></a></h2>
						<span class="attr">- By</span> <span class="view">:&nbsp; <?php echo $entry->author->name; ?> </span>
						<span class="attr">- Duration</span> <span class="view">:&nbsp;<?php printf('%0.2f', $length/60); ?> </span>
						<span class="attr"> - Views</span> <span class="view">:&nbsp;<?php echo $viewCount; ?></span>
						<p><?php echo substr($result['description'],0,200) . "...<p/>\n"; ?></p>
					</div>
				</li>
			<?php }}
			else{ ;?>
				<li>Empty reseult</li>
			<?php }
			//}
		//catch (Exception $e){
		//	echo "<li>Caught exception".$e->getMessage()."</li>";
		//}
		;?>
		</ul>
		
		<?php }
		//Load video with top five (categories)
		else{
			// iterate over category list
				$test_old =0;
				foreach ($categories as $c) {
					$test_old ++;
				  // for each category    
				  // set feed URL
				  $feedURL = "http://gdata.youtube.com/feeds/api/videos/-/{$c['term']}?max-results=5&orderby=viewCount";
				  
				  // read feed into SimpleXML object
				  $sxml = simplexml_load_file($feedURL);
				  
				  // get summary counts from opensearch: namespace
				  $counts = $sxml->children('http://a9.com/-/spec/opensearchrss/1.0/');
				  $total = $counts->totalResults; 
				  if($total != 0){
				  ?>
				  
				  <h2 class="result_search"><?php echo $c['label']; ?></h2>
				  <?php echo $total; ?> items found.
				  <p/>
				  
				  <?php    
				  echo "<ul>";
				  // iterate over entries in category
				  // print each entry's details
				  foreach ($sxml->entry as $entry) {
					// get nodes in media: namespace for media information
					$media = $entry->children('http://search.yahoo.com/mrss/');
					
					// get video player URL
					$attrs = $media->group->player->attributes();
					$watch = $attrs['url']; 
					 // get video thumbnail
					  $attrs = $media->group->thumbnail[0]->attributes();
					  $thumbnail = $attrs['url']; 
					// get <yt:duration> node for video length
					$yt = $media->children('http://gdata.youtube.com/schemas/2007');
					$attrs = $yt->duration->attributes();
					$length = $attrs['seconds']; 
					// get <yt:stats> node for viewer statistics
					$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
					$attrs = $yt->statistics->attributes();
					$viewCount = $attrs['viewCount']; 
					// get <gd:rating> node for video ratings
					$gd = $entry->children('http://schemas.google.com/g/2005'); 
					if ($gd->rating) {
					  $attrs = $gd->rating->attributes();
					  $rating = $attrs['average']; 
					} else {
					  $rating = 0; 
					}
					$str = '&feature=youtube_gdata_player';
					$replate = '&amp;hl=en&amp;fs=1&amp;autoplay=1;loop =1;repeat=1';
					$watch_video = str_replace($str,$replate,$watch,$count);
					$str = 'http://www.youtube.com/watch?v=';
					$replate = 'http://www.youtube.com/v/';
					$watch_video = str_replace($str,$replate,$watch_video,$count);
					?>
					<li href="#top">
						<a href="<?php echo $watch_video;?>"><img src="<?php echo $thumbnail;?>" /></a>
						<div class="block">
							<h2><a href="<?php echo $result['url']; ?>"><?php echo substr($result['title'],0,50)."..."; ?></a></h2>
							<span class="attr">- By</span> <span class="view">:&nbsp; <?php echo $entry->author->name; ?> </span>
							<span class="attr">- Duration</span> <span class="view">:&nbsp;<?php printf('%0.2f', $length/60); ?> </span>
							<span class="attr"> - Views</span> <span class="view">:&nbsp;<?php echo $viewCount; ?></span>
							<p><?php echo substr($media->group->description,0,200) . "...<p/>\n";?></p>
						</div>
					</li>
					<?php
				  }
				 /* if(($test_old%2) != 0){
					echo '<li>&nbsp;</li>';
				  }
				  $test_old =0;*/
				  echo "</ul>";
				}}
				}
		 ;?>
</div>
